package com.offcn.controller;

import com.github.pagehelper.PageInfo;
import com.offcn.bean.Blog;
import com.offcn.service.BlogService;
import com.offcn.utils.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("blog")
@ResponseBody
public class BlogController {
    private ReturnResult returnResult;
    @Autowired
    private BlogService blogService;

    //添加博客
    @RequestMapping("addBlog")
    public ReturnResult addBlog(Blog blog, HttpSession session){
        System.out.println(blog);
        ReturnResult returnResult = blogService.addBlog(blog, session);
        return returnResult;
    }

    //查看博客详情时查询用户数据
    @RequestMapping("getBlogUserByid")
    public Blog getBlogUserByid(Integer bid){
        Blog blogUserByid = blogService.getBlogUserByid(bid);
        return blogUserByid;
    }

    //获取博客列表
    @RequestMapping("getBlogPage")
    public PageInfo<Blog> getBlogPage(@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo){
        PageInfo<Blog> blogPage = blogService.getBlogPage(pageNo);
        return blogPage;
    }
    //删除博客
    @RequestMapping("deleteBlogById")
    public ReturnResult deleteBlogById(Integer bid,String btitle){
        try {
            blogService.deleteBlogById(bid);
            returnResult = new ReturnResult("【"+btitle+"】删除成功!",1,null);
        } catch (Exception e) {
            e.printStackTrace();
            returnResult = new ReturnResult("【"+btitle+"】删除失败!",-1,null);
        }
        return returnResult;
    }
}
